{include file='public/header' /}


<body>

    <div class="layui-fluid">

        <div class="layui-row">

            <form class="layui-form " enctype="multipart/form-data" id="file" style="margin: 50px;">


                <div class="layui-form-item">

                    <label class="layui-form-label" for="name">

                        <span class="x-red">

                            *

                        </span>

                        角色名称

                    </label>

                    <div class="layui-input-block ">

                        <input autocomplete="off" class="layui-input" id="name" lay-verify="required" name="name" placeholder="请输入角色名称" required="" type="text">
                    </div>

                </div>



                 <div class="layui-form-item">
                          <label for="phone" class="layui-form-label">
                              备注信息
                          </label>
                           <div class="layui-input-block">
                            <textarea placeholder="请输入备注信息" class="layui-textarea" name="remark"></textarea>
                          </div>
                  </div>



                
                <div class="layui-form-item" id="quanxian">
                  <label for="phone" class="layui-form-label">
                     <span class="x-red">*</span>
                      权限
                  </label>
                    <fieldset class="layui-elem-field" style="padding:20px 0px;">
                         <legend>权限分配</legend>
                         <div class="layui-input-block" style="width:65%; margin-left: 0px;">
                            <div id="test12" class="demo-tree-more"></div>
                          </div>
                   </fieldset>
              </div> 

              
                <div class="layui-form-item" style="margin-top: 50px;">

                    <label class="layui-form-label" for="L_repass">

                    </label>

                    <button class="layui-btn" id="submitBtn" lay-filter="add" lay-submit="" type="submit">

                        添加角色

                    </button>

                </div>

            </form>

        </div>

    </div>






    <script>

        layui.use(['form', 'layer'],

    function() {

        $ = layui.jquery;

        var form = layui.form,

        layer = layui.layer;


    });

    </script>



    <script>
layui.use(['tree', 'util'], function(){
  var tree = layui.tree
  ,layer = layui.layer
  ,util = layui.util
  
 
  
 
  //基本演示
  tree.render({
    elem: '#test12'
    ,data: {$mulu1}
    ,showCheckbox: true  //是否显示复选框
    ,id: 'demoId1'
    ,isJump: true //是否允许点击节点时弹出新窗口跳转
  });


    //按钮事件
  util.event('lay-demo', {
    getChecked: function(othis){
      var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
      
      layer.alert(JSON.stringify(checkedData), {shade:0});
      console.log(checkedData);
    }
    ,setChecked: function(){
      tree.setChecked('demoId1', [12, 16]); //勾选指定节点
    }
    ,reload: function(){
      //重载实例
      tree.reload('demoId1', {
        
      });
      
    }
  });
  
});
</script>




    <script>

    layui.config({
        base: '__ROOT__/static/admin/lib/layui/lay/modules/',
    })
    // 只需要引入multiSelect模块即可
    layui.use(['multiSelect'],function() {
        
    });
        //获取form表单的节点

  fileEle = document.getElementById('file');

  arr = [];

  //把value的值拿出来

  valEle = '';

  nulEle = '';





  $("#file").on("submit",function(){

   

    //阻止form默认提交

    var evt = event;

    evt.preventDefault();

    

    //得到formData

    var formData = new FormData($(this)[0]);


    layui.use(['tree', 'util'], function(){
    var tree = layui.tree
    ,layer = layui.layer
    ,util = layui.util

      var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据

      var nodeIds = new Array();
      nodeIds = getCheckedId(checkedData);
      
      formData.append('idstr', nodeIds);
       
      // 获取选中节点的id
      function getCheckedId(jsonObj) {
          var id = "";
          $.each(jsonObj, function (index, item) {
              if (id != "") {
                  id = id + "," + item.id;
              }
              else {
                  id = item.id;
              }
              var i = getCheckedId(item.children);
              if (i != "") {
                  id = id + "," + i;
              }
          });
          return id;
      }


    });
    // $("#submitBtn").attr("disabled","disabled");


    $.ajax({

      url:"{:url('Role/add')}",

      type:"post",

      async: false,

      cache: false,

      contentType: false,

      processData: false,

      data:formData,

      success:function(data){


        if (data == 1) {

            layer.msg('添加成功', {icon: 1,time: 1500});

            setTimeout(function(){
              //刷新父页面
              window.parent.location.reload(); 
              // 获得frame索引
              var index = parent.layer.getFrameIndex(window.name); 
              //关闭当前frame
              parent.layer.close(index);

            },1500);

        }else if(data == 2){

            layer.msg('添加失败,请完善全部信息！', {icon: 2,time: 1500});

        }else{

            layer.msg('添加失败', {icon: 2,time: 1500});


        }

        

      }

    })

  })

    </script>

</body>

